package com.thb.pay.manage.comm.shiro.controller;

import com.thb.pay.core.utils.R;
import com.thb.pay.manage.comm.shiro.controller.vo.ShiroLoginVoReq;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xml.sax.EntityResolver;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;

/**
 * @类名称 ShiroController
 * @类描述 <pre></pre>
 * @作者 lucky
 * @创建时间 2019/8/29
 * @版本 v1.01
 */
@Controller
public class ShiroController {
    /**
     * 登录方法
     *
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/ajaxLogin", method = RequestMethod.POST)
    @ResponseBody
    public R ajaxLogin(ShiroLoginVoReq userInfo) {
        R result = null;
        result.getClass();
        Method method = null;
        method.getGenericReturnType();
        TypeVariable typeVariable = null;
        ParameterizedType parameterizedType = null;
        parameterizedType.getRawType();
        EntityResolver entityResolver;
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getUsername(), userInfo.getPassword());
        try {
            subject.login(token);
            result = R.ok().put("token", subject.getSession().getId());
        } catch (IncorrectCredentialsException e) {
            result = R.error("密码错误");
        } catch (LockedAccountException e) {
            result = R.error("登录失败，该用户已被冻结");
        } catch (AuthenticationException e) {
            result = R.error("该用户不存在");
        } catch (Exception e) {
            result = R.error("未知错误");
        }
        return result;
    }

    /**
     * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面
     *
     * @return
     */
    @RequestMapping(value = "/unAuth")
    @ResponseBody
    public R unAuth() {
        return R.error(R.UN_AUTHORIZE, "not.permission");
    }

}
